package com.jeff.android.lab.module.anim.path;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author: duff
 * @date: 2019/1/3
 * @since: 1.0.0
 */
public class MyPathView extends View {
    private Paint mPaint;
    private Path mPath;

    private int mWidth = 0;
    private int mHeight = 0;

    public MyPathView(Context context) {
        super(context);
        init();
    }

    public MyPathView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyPathView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#00aff9"));           // 画笔颜色 - 黑色
        mPaint.setStyle(Paint.Style.STROKE);    // 填充模式 - 描边
        mPaint.setStrokeWidth(5);              // 边框宽度

        mPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.translate(mWidth / 2, mHeight / 2); // 移动坐标到屏幕中心
//
//        drawRect();
//        drawCircle();
//        drawOval();
//        drawArc();

//        drawRectAndCircle();

//        drawBezier();

//        canvas.drawPath(mPath, mPaint);

//        drawCircleOffset(canvas);

//        drawWave(canvas);

        drawGradient();

        canvas.drawPath(mPath, mPaint);
    }

    // 矩形
    private void drawRect() {
        mPath.addRect(-300, -100, 300, 100, Path.Direction.CW);

        LinearGradient lg = new LinearGradient(-300, -100, 300, -100,
                Color.parseColor("#00AFF9"), Color.parseColor("#057AFF"), Shader.TileMode.CLAMP);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setShader(lg);
    }

    // 圆形
    private void drawCircle() {
        mPath.addCircle(0, 0, 200, Path.Direction.CW);
    }

    // 椭圆
    private void drawOval() {
        RectF rectF = new RectF(-200, -100, 200, 100);
        mPath.addOval(rectF, Path.Direction.CW);
    }

    //
    private void drawArc() {
        RectF rectF = new RectF(0, 0, 300, -300);
        mPath.addArc(rectF, 0, 90);
        mPath.arcTo(rectF, 0, 90);
    }

    // Path 合并
    private void drawRectAndCircle() {
        mPath.addRect(-100, -200, 100, 200, Path.Direction.CW);

        Path src = new Path();
        src.addCircle(0, 0, 100, Path.Direction.CW);

        mPath.addPath(src, 0, -200);
    }

    // offset
    private void drawCircleOffset(Canvas canvas) {
        mPath.addCircle(0, 0, 100, Path.Direction.CW);

        Path dst = new Path();
        mPath.offset(300, 0, dst);

        canvas.drawPath(mPath, mPaint);

        mPaint.setColor(Color.RED);
        canvas.drawPath(dst, mPaint);
    }

    // 贝塞尔曲线

    // second order
    private void drawBezier() {
//        mPath.cubicTo(0, 0, 100, 300, 500, 500);
//        mPath.quadTo(mWidth / 4, -150, mWidth / 2, 0);
//        mPath.quadTo(mWidth * 3 / 4, 150, mWidth, 0);

        mPath.rQuadTo(mWidth / 4, -150, mWidth / 2, 0);
        mPath.rQuadTo(mWidth / 4, 150, mWidth / 2, 0);
    }

    private void drawWave(Canvas canvas) {
        mPath.reset();

        
    }
    // three order



    private void drawGradient() {
        mPath.moveTo(0, 500);
        mPath.quadTo(getWidth()/2, 800, getWidth(), 500);

        mPath.lineTo(getWidth(), 0);
        mPath.lineTo(0, 0);
        mPath.close();

        mPaint.setStyle(Paint.Style.FILL);

        LinearGradient lg = new LinearGradient(0, 0, getWidth(), 0,
                Color.parseColor("#00AFF9"), Color.parseColor("#057AFF"), Shader.TileMode.CLAMP);
        mPaint.setShader(lg);
    }

}
